package com.behome.netty.common.util;

import com.behome.netty.common.ByteConventerKit;

public class CRCKit {
    private static final int CN_CRC32 = 79764919;
    private static int[] ptiTableCRC32 = new int[256];

    public CRCKit() {
    }

    private static void build32(int cn) {

        for(int i = 0; i < 256; ++i) {
            int nData = i << 24;
            int nAccum = 0;
            for(int j = 0; j < 8; ++j) {
                if(0 != ((nData ^ nAccum) & -2147483648)) {
                    nAccum = nAccum << 1 ^ cn;
                } else {
                    nAccum <<= 1;
                }

                nData <<= 1;
            }

            ptiTableCRC32[i] = nAccum;
        }

    }

    public static int crc32Calc(byte[] datas) {
        int crc32 = -1;
        if(null != datas && datas.length >= 1) {
            byte[] var2 = datas;
            int var3 = datas.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                byte data = var2[var4];
                crc32 = crc32 << 8 ^ ptiTableCRC32[crc32 >>> 24 ^ data & 255];
            }

            return crc32;
        } else {
            return crc32;
        }
    }

    public static int crc16Calc(byte[] bytes) {
        int crc = '\uffff';
        int polynomial = 4129;

        for(int index = 0; index < bytes.length; ++index) {
            byte b = bytes[index];

            for(int i = 0; i < 8; ++i) {
                boolean bit = (b >> 7 - i & 1) == 1;
                boolean c15 = (crc >> 15 & 1) == 1;
                crc <<= 1;
                if(c15 ^ bit) {
                    crc ^= polynomial;
                }
            }
        }

        crc &= '\uffff';
        return crc;
    }

    public static void main(String[] args) {

        String a="";
        String s="4900010401000000eef451068100000001010101012021110335350104313142303104617BA460618552A85001010cf051068100000003140000000163686e000084b2e2cad4d3a6bcb1b9e3b2a5b2e2cad4d3a6bcb1b9e3b2a5b2e2cad4d3a6bcb1b9e3b2a5b2e2cad4d3a6bcb1b9e3b2a5b2e2cad4d3a6bcb1b9e3b2a5b2e2cad4d3a6bcb1b9e3b2a5b2e2cad4d3a6bcb1b9e3b2a5b2e2cad4d3a6bcb1b9e3b2a5b2e2cad4d3a6bcb1b9e3b2a5b2e2cad4d3a6bcb1b9e3b2a5b2e2cad4d3a6bcb1b9e3b2a50ecfd8d3a6bcb1b9e3b2a5c6bdcca8013d000000147274703a2f2f3232352e302e302e313a353030300504010203040000004a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
        System.out.println(s.length());
        System.out.println(s.substring(644,646));
        for (int i = 0; i <s.length() ; i++) {
            if(i%2==0){
               a=a+" "+s.substring(i,i+2);
            }
        }
        System.out.println(a);
        byte[] test = ByteConventerKit.hexStrWithWhiteSpaceTobytes("49 00 01 04 01 00 00 00 3B F4 51 06 81 00 00 00 01 01 01 01 01 20 21 11 03 35 35 02 05 36 30 30 30 30 04 60 76 B6 E0 60 76 BD E8 50 01 01 0C F0 51 33 32 00 00 00 03 14 00 00 00 00 04 04 01 02 03 04 00 00 00 4A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ");
        byte[] crc32 = ByteConventerKit.intToBytes2(crc32Calc(test));
        System.out.println(ByteConventerKit.bytesToHexStr(crc32, false));
    }

    static {
        build32(CN_CRC32);
    }
}
